#include "stdafx.h"
#include "Helpers.h"

void LogEvent(LPCTSTR pFormat, ...)
{
    FILE *fp;

    CHAR chMsg[256] = {0};
    va_list pArg;

    va_start(pArg, pFormat);
    _vstprintf(chMsg, pFormat, pArg);
    va_end(pArg);

    fp = fopen("c:\\log.txt", "a+");
    if (fp)
    {    
        _fputts(chMsg, fp);
        fclose(fp);
    }
}

BOOL TCPReceive(SOCKET hSocket, PCHAR pBuff, DWORD dwBytesToReceive)
{
    UINT i = 0;
    INT nRet;

    while (dwBytesToReceive > 0)
    {
        nRet = recv(hSocket, &pBuff[i], dwBytesToReceive, 0);
        if (nRet == 0 || nRet == SOCKET_ERROR)
        {
            return FALSE;
        }

        i += nRet;
        dwBytesToReceive -= nRet;
    }

    return TRUE;
}

BOOL TCPSend(SOCKET hSocket, PCHAR pBuff, DWORD dwBytesToSend)
{
    UINT i = 0;
    INT nRet;

    while (dwBytesToSend > 0)
    {
        nRet = send(hSocket, &pBuff[i], dwBytesToSend, 0);
        if (nRet == SOCKET_ERROR)
        {
            return FALSE;
        }

        i += nRet;
        dwBytesToSend -= nRet;
    }

    return TRUE;
}